package cn.wind.xboot.module.web.controller.sys;

import cn.wind.xboot.core.annotation.RestJsonController;
import cn.wind.xboot.core.res.ApiResult;
import cn.wind.xboot.logic.sys.entity.SysDictData;
import cn.wind.mybatis.utils.SearchUtil;
import cn.wind.xboot.framework.domain.PageVo;
import cn.wind.xboot.framework.domain.SearchVo;
import cn.wind.xboot.framework.web.controller.BaseController;
import cn.wind.xboot.module.web.vo.sys.DictDataVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>Title: DictController</p>
 * <p>Description: TODO</p>
 *
 * @author xukk
 * @version 1.0
 * @date 2018/8/7
 */
@Slf4j
@RestJsonController
@Api(value = "dict", tags = "字典管理接口")
@RequestMapping("dict/data")
@Validated
public class DictDataController extends BaseController<SysDictData, Long> {


    @Override
    public IService getService() {
        return sysDictDataService;
    }

    @GetMapping
    public ApiResult get(DictDataVo dictDataVo, PageVo pageVo, SearchVo searchVo) {
        return ApiResult.Custom().success().add(super.get(pageVo.init(),
                SearchUtil.autoBuildQuery(beanMapper.map(beanMapper.map(dictDataVo, SysDictData.class), Map.class),
                        new QueryWrapper(), SysDictData.class)));
    }

    @PostMapping
    public ApiResult save(DictDataVo entity) {
        return save(beanMapper.map(entity, SysDictData.class));
    }

    @PutMapping
    public ApiResult edit(DictDataVo entity) {
        SysDictData sysDictData = beanMapper.map(entity, SysDictData.class);
        return super.edit(sysDictData, new QueryWrapper<SysDictData>().lambda()
                .eq(SysDictData::getId, entity.getId()));
    }

    @Override
    public ApiResult delete(@RequestParam Long[] ids) {
        return super.delete(ids);
    }
}
